package vnreal.demands;

import vnreal.ExchangeParameter;
import vnreal.algorithms.utils.MiscelFunctions;
import vnreal.constraints.AbstractConstraint;
import vnreal.constraints.ILinkConstraint;
import vnreal.network.Link;
import vnreal.resources.AbstractResource;
import vnreal.resources.BandwidthResource;
import vnreal.resources.Length;
import vnreal.resources.ResourceVisitorAdapter;

public final class Delay extends AbstractDemand implements
		ILinkConstraint {
	private double demandedDelay;

	public Delay(Link<? extends AbstractConstraint> owner) {
		super(owner);
	}

//	@ExchangeParameter
//	public void setDemandedBandwidth(Double bandwidth) {
//		this.demandedBandwidth = bandwidth;
//	}

	@ExchangeParameter
	public Double getDemandedBandwidth() {
		return demandedDelay;
	}

	@Override
	protected ResourceVisitorAdapter createAcceptsVisitor() {
		return new ResourceVisitorAdapter() {
			@Override
			public boolean visit(Length res) {
				return true;
			}
		};
	}

	@Override
	protected ResourceVisitorAdapter createFulfillsVisitor() {
		return new ResourceVisitorAdapter() {
			@Override
			public boolean visit(BandwidthResource res) {
				return MiscelFunctions.roundThreeDecimals(res
						.getAvailableBandwidth()) >= MiscelFunctions
						.roundThreeDecimals(getDemandedBandwidth());
			}
		};
	}

	@Override
	public boolean occupy(AbstractResource res) {
		return res.getOccupyVisitor().visit(this);
	}

	@Override
	public boolean free(AbstractResource res) {
		return res.getFreeVisitor().visit(this);
	}

	@Override
	public String toString() {
		return new String("Link dealy="
				+ getDemandedBandwidth() + "us");
	}
}